package org.gitman.agent.gui;

import java.awt.Color;
import java.util.LinkedList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.gitman.agent.platform.Agent;
import org.gitman.agent.platform.PlatformManager;

public class AgentTableModel extends AbstractTableModel {
		
	private static String [] columns = {"id", "Name", "start", "end", "State"};
	private List<Agent> agents;
	private PlatformManager platformManager;
	
	public AgentTableModel(PlatformManager platformManager){
		this.platformManager = platformManager;
		agents = platformManager.getSimulation().getAgentManager().getAgents();
	}
	
	@Override
	public int getColumnCount() {
		return columns.length;
	}

	@Override
	public int getRowCount() {
		return agents.size();
	}
	
	@Override
	public String getColumnName(int arg0) {
		return columns[arg0];
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	@Override
	public Object getValueAt(int arg0, int arg1) {
		Agent agent = agents.get(arg0);
		switch (arg1) {
		case 0:
			return agent.getColor();			
		case 1:
			return agent.getName();			
		case 2:
			return agent.getStartPoint().getValue().toString();
		case 3:
			return agent.getEndPoint().getValue().toString();
		case 4:
			return agent.getState().toString();			
		default:
			return new String("###");
		}
	}

	@Override
	public void fireTableDataChanged() {
		agents = platformManager.getSimulation().getAgentManager().getAgents();
		super.fireTableDataChanged();
	}

	@Override
	public Class<?> getColumnClass(int arg0) {
		switch (arg0) {
		case 0:
			return Color.class;
		default:
			return String.class;
		}
	}

	public void removeAgents(int [] tab) {
		List<Agent> a2rem = new LinkedList<Agent>();
		for(int i = 0;i<tab.length;i++){
			a2rem.add(agents.get(tab[i]));
		}
		for(Agent a : a2rem){
			platformManager.getSimulation().getAgentManager().removeAgent(a);
		}
	}
	
	public Agent getAgentAt(int index){
		return agents.get(index);
	}
}
